package cn.gson.crmdemo.model.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.CkDao;
import cn.gson.crmdemo.model.dao.ProductDao;
import cn.gson.crmdemo.model.dao.PurchaseDao;
import cn.gson.crmdemo.model.mapper.CkMapper;
import cn.gson.crmdemo.model.pojo.Purchase;
import cn.gson.crmdemo.model.pojo.Warehouse;
import cn.gson.crmdemo.model.vo.KcVo;



@Service
@Transactional(rollbackFor=Exception.class)
public class CkService {
	@Autowired
	CkDao ck;
	@Autowired
	ProductDao pdao;
	@Autowired
	PurchaseDao pd;
	@Autowired
	CkMapper Ck2;
	
	/*
	 * 查询仓库
	 */

	public JSONArray selectWarehouse(String objects,Integer selectType) {		
		return Ck2.selectWarehouse(objects,selectType);
	}
	
	
	/*
	 * 新增入库
	 */
	public void addck(KcVo rkd) {
		List<Integer> list = Ck2.selectWarehousecpId(rkd.getCpId());
		Warehouse war = new Warehouse();	
		if(list.size()<=0) {
			war.setCkNumber(rkd.getCkNumber());
			war.setRkDate(rkd.getRkDate());
			war.setProduct(pdao.findById(rkd.getCpId()).get());
			ck.save(war);
		}
		else {
			Warehouse wr = ck.findById(list.get(0)).get();
			wr.setCkNumber(wr.getCkNumber()+rkd.getCkNumber());
		}
		
	}
	
	/**
	 * 查询商品和数量
	 * @return
	 */
	public JSONArray selectProcurementDetails(int a) {		
		Purchase cgd =pd.findById(a).get();
		cgd.setPurchaseState(3);
		pd.save(cgd);
		return Ck2.selectProcurementDetails(a);
	}
	
}
